var status = 0;
var icon1 = "#fEffect/CharacterEff/1042176/0/0#";
var icon3 = "#fEffect/CharacterEff/1082565/4/0#";
var icon2 = "#fEffect/CharacterEff/1082565/2/0#";
var icon1 = "#fEffect/CharacterEff/1082565/4/0#";
var icon2 = "#fEffect/CharacterEff/1082565/2/0#";
var aaa = "#fUI/UIWindow.img/PvP/Scroll/enabled/next2#";
var icon = "#fUI/UIWindow.img/PvP/Scroll/enabled/next2#";
var iconEvent = "#fUI/UIWindow.img/PvP/Scroll/enabled/next2#";
var icon2 = "#fUI/UIToolTip.img/Item/Equip/Star/Star#";
var aaa = "#fUI/UIWindow/AriantMatch/characterIcon/5#";
var yun = "#fUI/UIWindow/Quest/icon8/0#";
var yun2 = "#fUI/UIWindow/Quest/icon8/0#";
var yun8 = "#fUI/UIWindow/MonsterBook/arrowLeft/normal/0#";
var yun9 = "#fUI/UIWindow/MonsterBook/arrowRight/normal/0#";
var yun4 = "#fUI/UIWindow/Quest/reward#";
var ttt = "#fUI/UIWindow/Quest/icon2/7#";
var ttt2 = "#fUI/UIWindow/Quest/icon6/7#";
var ttt3 = "#fUI/UIWindow/Quest/icon3/6#";
var ttt4 = "#fUI/UIWindow/Quest/icon5/1#";
var ttt5 = "#fUI/UIWindow/Quest/icon0#";
var ttt7 = "#fUI/Basic/BtHide3/mouseOver/0#";
var ttt6 = "#fUI/UIWindow.img/PvP/Scroll/enabled/next2#";
var epp = "#fEffect/CharacterEff/1082312/0/0#";
var eff = "#fCharacter/Weapon/01702523.img/48/straight/0/effect#";
var eff = "#fEffect/CharacterEff/1112905/0/1#";
var epp = "#fEffect/CharacterEff/1082312/0/0#";
var epp1 = "#fEffect/CharacterEff/1082312/2/0#";
var axx = "#fEffect/CharacterEff/1051294/0/0#";
var xxx = "#fEffect/CharacterEff/1082565/2/0#";
var ppp = "#fEffect/CharacterEff/1112907/4/0#";
var epp3 = "#fEffect/CharacterEff/1112908/0/1#";
var axx1 = "#fEffect/CharacterEff/1062114/1/0#";
var zs = "#fEffect/CharacterEff/1112946/2/0#";
var zs1 = "#fEffect/CharacterEff/1112946/1/1#";
var dxxx = "#fEffect/CharacterEff/1102232/2/0#";
var tz = "#fEffect/CharacterEff/1082565/2/0#";
var tz1 = "#fEffect/CharacterEff/1082565/4/0#";
var tz7 = "#fEffect/CharacterEff/1112900/3/1#";
var tz8 = "#fEffect/CharacterEff/1112900/4/1#";
var tz88 = "#fEffect/CharacterEff/1112900/5/1#";
var yun1 = "#fUI/UIWindow/Quest/icon7/10#";
var tz9 = "#fEffect/CharacterEff/1112902/0/0#";
var tz10 = "#fEffect/CharacterEff/1112903/0/0#";
var tz11 = "#fEffect/CharacterEff/1112904/0/0#";
var tz12 = "#fEffect/CharacterEff/1112924/0/0#";
var tz13 = "#fEffect/CharacterEff/1112925/0/0#";
var tz14 = "#fEffect/CharacterEff/1112926/0/0#";
var tz15 = "#fEffect/CharacterEff/1112949/0/0#";
var tz16 = "#fEffect/CharacterEff/1112949/1/0#";
var tz17 = "#fEffect/CharacterEff/1112949/2/0#";
var tz18 = "#fEffect/CharacterEff/1112949/3/0#";
var tz19 = "#fEffect/CharacterEff/1112949/4/0#";
var tz20 = "#fEffect/CharacterEff/1114000/1/0#";
var p3 = "#fUI/UIToolTip.img/Item/Equip/Star/Star#";
var p4 = "#fUI/StarCityUI.img/GradeInfo/icon_ss/7#";
var p1 = "#fUI/SoulUI.img/DungeonMap/icon/soulTrap/0#";
var p = "#fUI/StarCityUI.img/GradeInfo/icon_ss/2#";
var q6 = "#fUI/RunnerGame.img/RunnerGameUI/UI/Point/2#";
var p2 = "#fUI/SoulUI.img/DungeonMap/icon/dungeonItem/0#";
var e2 = "#fUI/Basic.img/HScr7/enabled/base#";
var e1 = "#fUI/Basic.img/HScr7/enabled/next0#";
var e3 = "#fUI/Basic.img/HScr7/enabled/prev0#";
var icon2 = "#fUI/UIToolTip.img/Item/Equip/Star/Star#";
var head = "#fUI/UIWindow2.img/Quest/quest_info/summary_icon/summary#\r\n";
var gardenData = null;
var gardenFlowerPot = null;
var typed = -1;
var operationId = 0;
var buyFlower = null;

function start() {
    status = -1;
    action(1, 0, 0)
}

function action(q, c, y) {
    if (status == 0 && q == 0) {
        cm.dispose();
        return
    }
    if (typed == 5 && q == 0) {
        status = 2;
        typed = 0
    }
    if (q == 1) {
        status++
    } else {
        status--
    }
    if (status == 0) {
        gardenData = getGardenInfo();
        var n = "\t" + axx + "#r#e" + cm.getServerName() + "花园 - 培养之室" + axx + "\r\n\r\n\r\n";
        n += "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "#k#k\r\n\r\n";
        n += "\t\t\t" + icon1 + " #e#d花园等级：#r[" + gardenData.level + "]#d 级  \r\n";
        n += "\t\t\t" + icon1 + " #e#d活力：#r[" + cm.getPlayerEnergy() + "]#d 点\r\n";
        n += "\t\t\t" + icon1 + " #e#d经验值：#r[" + gardenData.exp + "/" + calcLevelUp(gardenData.level) + "]#k\r\n\r\n";
        n += "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "" + tz13 + "" + tz14 + "" + tz20 + "#k#k\r\n";
        n += "  #b#L0#" + icon2 + " 照顾花朵#l";
        n += "#b#L1#" + icon2 + " 扩建花园#l";
        n += "#b#L2#" + icon2 + " 购买种子#l";
        cm.askMenu(n)
    } else {
        if (status == 1) {
            if (y == -1 && q == 0) {
                y = typed
            }
            if (y == -1 && q != 0) {
                y = 0
            }
            switch (y) {
                case 0:
                    typed = 0;
                    gardenFlowerPot = getFlowerPot();
                    var s = new Date().getTime();
                    var n = "\t\t\t\t\t#d#e★ 我的花园 ★#n#k\r\n";
                    for (var x in gardenFlowerPot) {
                        var i = gardenFlowerPot[x]["seedid"];
                        var o = gardenFlowerPot[x]["id"];
                        var w = gardenFlowerPot[x]["expiration"];
                        if (i != 0) {
                            var a = "";
                            var m = (w - s) / 1000;
                            if (m <= 0) {
                                a = "#g作物已经成熟#k"
                            } else {
                                var l = toDHM(m);
                                a = "#b剩余 " + l[0] + "天" + l[1] + "小时" + l[2] + "分"
                            }
                            n += "#L" + o + "#" + icon1 + " #r[#t" + i + "#]#k " + a + "#l\r\n"
                        } else {
                            n += "#L" + o + "#" + icon1 + " #b[这是一个空的花盆，点击栽种]\r\n"
                        }
                    }
                    cm.sendOkS(n);
                    break;
                case 1:
                    typed = 1;
                    cm.askYesNo("扩建花园需要#r1000W#k，扩建之后将会多出一个花盆，是否扩建？");
                    break;
                case 2:
                    typed = 2;
                    var n = head + "\r\n#e— 物品购物 -\r\n\r\n";
                    n += " #b以下是您的等级可以购买的种子列表：#n#k\r\n";
                    var b = getFlowers();
                    for (var x in b) {
                        var a = b[x][1] + "#n#b级";
                        if (b[x][1] > gardenData.level) {
                            a = "(需要" + b[x][1] + "级)#n#b"
                        }
                        n += "#b#L" + x + "##v" + b[x][0] + "# #t" + b[x][0] + "#种子 #r#e" + a + " (产量：" + b[x][4] + "~" + b[x][5] + ")#l\r\n"
                    }
                    cm.sendOkS(n);
                    break
            }
        } else {
            if (status == 2) {
                if (typed == 0) {
                    var s = new Date().getTime();
                    var o = y;
                    operationId = o;
                    var i = gardenFlowerPot[o]["seedid"];
                    var w = gardenFlowerPot[o]["expiration"];
                    var n = "\t\t\t\t\t#d#e★ 我的花园 ★#n#k\r\n";
                    if (i != 0 && i != -1) {
                        var m = (w - s) / 1000;
                        var t = false;
                        var a = "";
                        if (m <= 0) {
                            t = true;
                            var a = "#g作物已经成熟，可以采摘#k"
                        } else {
                            var l = toDHM(m);
                            a = "#b剩余 " + l[0] + "天" + l[1] + "小时" + l[2] + "分#k"
                        }
                        n += "#r#t" + i + "##k\r\n";
                        n += a + "\r\n";
                        if (!t) {
                            n += "#b#L0#" + icon2 + "浇水#l\t#L1#" + icon2 + "施肥#l\t    #r#L2#" + icon2 + "挖除#l"
                        } else {
                            n += "#b#L4#" + icon2 + "采摘果实#l\t#L2#" + icon2 + "挖除#l"
                        }
                        cm.sendOkS(n)
                    } else {
                        n += "#r这是一个空的花盆，您可以进行栽种。\r\n";
                        n += "#b#L5#" + icon2 + "栽种#l";
                        cm.sendOkS(n)
                    }
                } else {
                    if (typed == 1) {
                        status -= 2;
                        if (cm.getMeso() >= 10000000) {
                            if (addFlowerPot()) {
                                cm.gainMeso(-10000000);
                                cm.sendOkS("扩建成功！\r\n#L0##b" + icon2 + "点我返回#l")
                            } else {
                                cm.sendOkS("扩建失败！您的等级不足或花盆数量已达到最大值，无法继续扩建，请先提升花园等级。\r\n#L0##b" + icon2 + "点我返回#l")
                            }
                        } else {
                            cm.sendOkS("您没有那么多游戏币！\r\n#L0##b" + icon2 + "点我返回#l")
                        }
                    } else {
                        if (typed == 2) {
                            var j = getFlowers();
                            j = j[y];
                            buyFlower = j;
                            var h = j[3] * 3600;
                            var l = toDHM(h);
                            var n = "\t\t\t\t\t#d#e★ 种子信息 ★#n#k\r\n";
                            n += icon3 + "#r品种：#b#v" + j[0] + "##t" + j[0] + "#种子\r\n";
                            n += icon3 + "#r产量：#b" + j[4] + "~" + j[5] + "\r\n";
                            n += icon3 + "#r单价：#b" + j[2] + " 游戏币\r\n";
                            n += icon3 + "#r成熟时间：#b" + l[0] + "天" + l[1] + "小时" + l[2] + "分#k\r\n\r\n";
                            n += icon3 + "#d#e请输入你要购买的数量：#n#k\r\n";
                            cm.askNumber(n, 0, 1, 100)
                        }
                    }
                }
            } else {
                if (status == 3) {
                    if (typed == 2) {
                        status -= 3;
                        var f = y;
                        var v = buyFlower[2] * f;
                        if (gardenData.level < buyFlower[1]) {
                            cm.sendOkS("您的花园等级不足，无法购买该种子。\r\n#L0##b" + icon2 + "点我返回#l")
                        } else {
                            if (cm.getMeso() >= v) {
                                cm.gainMeso(-v);
                                gainSeed(buyFlower[0], f);
                                cm.sendOkS("购买成功！\r\n#L0##b" + icon2 + "点我返回#l")
                            } else {
                                cm.sendOkS("游戏币不足，购买失败！\r\n#L0##b" + icon2 + "点我返回#l")
                            }
                        }
                    } else {
                        switch (y) {
                            case 0:
                                status -= 3;
                                if (cm.getBossLog("花园浇水" + operationId) > 0) {
                                    cm.sendOkS("啊，好像浇过水了，不能拔苗助长！\r\n#L0##b" + icon2 + "点我返回#l")
                                } else {
                                    if (cm.getPlayerEnergy() > 10) {
                                        cm.setBossLog("花园浇水" + operationId);
                                        cm.gainPlayerEnergy(-10);
                                        Operations(operationId, 0);
                                        cm.getPlayer().dropMessage(6, "消耗了10点活力，缩短了1小时成长时间。");
                                        cm.sendOkS("好开心，花儿又长大了一点！\r\n#L0##b" + icon2 + "点我返回#l")
                                    } else {
                                        cm.sendOkS("糟糕了，没有活力了！\r\n#L0##b" + icon2 + "点我返回#l")
                                    }
                                }
                                break;
                            case 1:
                                status -= 3;
                                if (cm.getBossLog("花园施肥" + operationId) > 0) {
                                    cm.sendOkS("啊，好像施过肥了，不能拔苗助长！\r\n#L0##b" + icon2 + "点我返回#l")
                                } else {
                                    if (cm.getPlayerEnergy() > 20) {
                                        cm.setBossLog("花园施肥" + operationId);
                                        cm.gainPlayerEnergy(-20);
                                        Operations(operationId, 1);
                                        cm.getPlayer().dropMessage(6, "消耗了20点活力，缩短了2小时成长时间。");
                                        cm.sendOkS("好开心，花儿又长大了一点！\r\n#L0##b" + icon2 + "点我返回#l")
                                    } else {
                                        cm.sendOkS("糟糕了，没有活力了！\r\n#L0##b" + icon2 + "点我返回#l")
                                    }
                                }
                                break;
                            case 2:
                                status -= 3;
                                clearFlowerPot(operationId);
                                cm.sendOkS("噢，挖好了，让我来种上新的种子吧！\r\n#L0##b" + icon2 + "点我返回#l");
                                break;
                            case 4:
                                status -= 3;
                                var s = new Date().getTime();
                                var i = gardenFlowerPot[operationId]["seedid"];
                                if (cm.getSpace(Math.floor(i / 1000000)) < 2) {
                                    cm.sendOk("你的包裹空间好像不够呢，整理一下再来采摘吧！");
                                    cm.dispose();
                                    return
                                }
                                var w = gardenFlowerPot[operationId]["expiration"];
                                var m = (w - s) / 1000;
                                var j = getFlower(i);
                                var r = Math.floor(Math.random() * (j[5] - j[4] + 1) + j[4]);
                                var g = j[1];
                                var e = g * 200;
                                if (m <= 0) {
                                    cm.gainItem(i, r);
                                    clearFlowerPot(operationId);
                                    var u = gainPlantExp(i);
                                    var d = "";
                                    if (u == -1) {
                                        d = "花园等级提升了#r1#k级！"
                                    } else {
                                        d = "得到了#r" + u + "#k点花园经验值"
                                    }
                                    cm.getPlayer().modifyCSPoints(1, e);
                                    cm.sendOkS("好开心，终于到了收获果实的季节！\r\n得到了#b" + r + "#k个#v" + i + "##b#t" + i + "##k\r\n得到了#r" + e + "#k点卷\r\n" + d + "\r\n#L0##b" + icon2 + "点我返回#l")
                                } else {
                                    cm.dispose();
                                    cm.sendOk("骚年，你真的要强行采摘吗？")
                                }
                                break;
                            case 5:
                                var k = getSeeds();
                                var n = "#d#e选择想要种下的种子：#n#k\r\n";
                                if (k != "" && k != null && k != Array()) {
                                    for (var x in k) {
                                        n += "#b#L" + k[x][0] + "#[#v" + k[x][0] + "##t" + k[x][0] + "#种子] 数量：[" + k[x][1] + "]个#l\r\n"
                                    }
                                    typed = 5;
                                    cm.sendOkS(n)
                                } else {
                                    status -= 3;
                                    n = "啊哦，好像没有种子了。得去买一些！\r\n#L0##b" + icon2 + "点我返回#l";
                                    cm.sendOkS(n)
                                }
                                break
                        }
                    }
                } else {
                    if (status == 4) {
                        status -= 4;
                        plantFlower(y, operationId);
                        n = "种好咯！快快长大吧！\r\n#L0##b" + icon2 + "点我返回#l";
                        cm.sendOkS(n)
                    }
                }
            }
        }
    }
}

function gainSeed(b, a) {
    if (a == null) {
        a = 1
    }
    var c = cm.getPlayer().getId();
    if (cm.queryForOneResult("SELECT * FROM memory_seedpackage WHERE charid = ? and seedid = ?", c, b).isEmpty()) {
        cm.updateSQL("INSERT INTO memory_seedpackage(charid, seedid, quantity) VALUES(?,?,?)", c, b, a)
    } else {
        cm.updateSQL("UPDATE memory_seedpackage SET quantity=quantity+? WHERE charid=? AND seedid=?", a, c, b)
    }
    return true
}

function getSeeds() {
    var c = cm.getPlayer().getId();
    var a = [];
    var b = "SELECT * FROM memory_seedpackage WHERE charid = ? and quantity>0 order by seedid desc";
    cm.queryForAllResults(b, c).forEach(function (d) {
        a.push([d.getString("seedid"), d.getString("quantity")])
    });
    return a
}

function isAllowUpgrade() {
    var d = cm.getPlayer().getId();
    var a = true;
    var b = cm.queryForOneResult("SELECT COUNT(id) as pn FROM memory_flowerpot WHERE charid = ?", d);
    if (!b.isEmpty()) {
        var c = b.getInt("pn");
        a = c < gardenData.level && c < 10
    }
    return a
}

function addFlowerPot() {
    var a = cm.getPlayer().getId();
    if (isAllowUpgrade()) {
        cm.updateSQL("INSERT INTO memory_flowerpot(charid, seedid, expiration) VALUES(?,NULL,-1)", a);
        return true
    } else {
        return false
    }
}

function plantFlower(a, d) {
    var g = cm.getPlayer().getId();
    var c = getFlower(a);
    var e = new Date().getTime();
    var b = c[3] * 3600 * 1000 + e;
    var f = "UPDATE memory_flowerpot p, memory_seedpackage s SET p.expiration=?, p.seedid=?, s.quantity=s.quantity-1 WHERE s.seedid=? and p.charid=s.charid and p.id=? and p.charid=?";
    cm.updateSQL(f, b, a, a, d, g)
}

function getFlowers() {
    return Array(Array(2001532, 1, 150000, 2, 5, 7), Array(2001534, 1, 150000, 3, 13, 15), Array(2001535, 1, 150000, 10, 50, 60), Array(2432392, 1, 1000000, 15, 3, 5), Array(4310088, 1, 1000000, 15, 15, 20), Array(5050000, 2, 5000000, 18, 5, 8), Array(4310030, 2, 2000000, 18, 10, 15), Array(5121003, 3, 3000000, 22, 1, 2), Array(5121004, 3, 3000000, 22, 1, 2), Array(5121005, 3, 3000000, 22, 1, 2), Array(4000000, 4, 30000000, 22, 220, 500), Array(2432353, 4, 20000000, 24, 6, 10), Array(5110000, 5, 3000000, 26, 1, 1), Array(2450020, 6, 6000000, 30, 1, 3), Array(4033356, 7, 3000000, 138, 1, 1), Array(4000082, 7, 3000000, 58, 2, 5), Array(2431739, 8, 5000000, 38, 1, 2), Array(2450081, 9, 5000000, 52, 1, 2), Array(2430112, 11, 1000000, 52, 5, 10), Array(2430481, 12, 1500000, 52, 5, 10), Array(2430915, 15, 1800000, 52, 5, 10), Array(5220040, 15, 50000000, 48, 40, 50), Array(2431893, 20, 3000000, 52, 5, 10))
}

function getFlowerLevel(b) {
    var a = getFlowers();
    for (var c in a) {
        if (a[c][0] == b) {
            return a[c][1]
        }
    }
}

function getFlower(b) {
    var a = getFlowers();
    for (var c in a) {
        if (a[c][0] == b) {
            return a[c]
        }
    }
}

function calcLevelUp(b) {
    var a = 10 + b * b;
    return a
}

function gainPlantExp(a) {
    var d = getFlowerLevel(a) * 10;
    var e = cm.getPlayer().getId();
    if ((gardenData.exp * 1 + d) >= calcLevelUp(gardenData.level)) {
        var b = (gardenData.exp * 1 + d) - calcLevelUp(gardenData.level);
        var c = "UPDATE memory_garden SET exp=?, level=level+1 WHERE charid=?";
        cm.updateSQL(c, b, e);
        return -1
    } else {
        var c = "UPDATE memory_garden SET exp=exp+? WHERE charid=?";
        cm.updateSQL(c, d, e);
        return d
    }
}

function clearFlowerPot(a) {
    var c = cm.getPlayer().getId();
    var b = "UPDATE memory_flowerpot SET expiration=-1, seedid=NULL WHERE id=? and charid=?";
    cm.resetBossLog("花园施肥" + a);
    cm.resetBossLog("花园浇水" + a);
    cm.updateSQL(b, a, c)
}

function Operations(a, b) {
    var c = (b + 1) * 60 * 60 * 1000;
    var e = cm.getPlayer().getId();
    var d = "UPDATE memory_flowerpot SET expiration=expiration-? WHERE id=? and charid=?";
    cm.updateSQL(d, c, a, e)
}

function getGardenInfo() {
    var d = cm.getPlayer().getId();
    var c = "SELECT * FROM memory_garden WHERE charid = ? limit 1";
    var a = cm.queryForOneResult(c, d);
    if (a.isEmpty()) {
        c = "INSERT INTO memory_garden(charid, level, exp) VALUES(?,1,0)";
        cm.updateSQL(c, d);
        return getGardenInfo()
    } else {
        var b = Array();
        b.level = a.getInt("level");
        b.exp = a.getInt("exp");
        return b
    }
}

function getFlowerPot() {
    var c = cm.getPlayer().getId();
    var a = [];
    var b = "SELECT * FROM memory_flowerpot WHERE charid = ?";
    cm.queryForAllResults(b, c).forEach(function (d) {
        var e = Array();
        e.id = d.getInt("id");
        e.seedid = d.getInt("seedid");
        e.expiration = d.getLong("expiration");
        a[e.id] = e
    });
    if (a.length == 0) {
        b = "INSERT INTO memory_flowerpot(charid, seedid, expiration) VALUES(?,NULL,-1)";
        cm.updateSQL(b, c);
        return getFlowerPot()
    }
    return a
}

function toDHM(c) {
    var d = Math.floor(c / 86400);
    var a = Math.floor(c % 86400 / 3600);
    var b = Math.floor(c % 86400 % 3600 / 60);
    return Array(d, a, b)
};